from pyppeteer import launch
import asyncio
import time

width = 1366
height = 768


async def run():
    # 代理设置  未使用
    proxies = '115.148.65.58:49530'

    # browser = await launch(headless=False, args=['--disable-infobars', '--proxy-server={}'.format(proxies)])
    browser = await launch(headless=False, args=[f'--window-size={width},{height}', '--disable-infobars'])
    page = await browser.newPage()
    await page.setViewport({'width': 1366, 'height': 768})
    await page.evaluate("""() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }""")
    await page.goto('http://mp.163.com/login.html')

    # 获取所有框架
    iframe = page.frames

    # 等待input出现
    await asyncio.sleep(3)
    await iframe[1].waitForXPath('//input')
    # 输入账号密码 进行登录
    # print(await iframe[1].xpath("//div[@class='yidun_slider']"))
    await iframe[1].type('[data-placeholder="网易邮箱帐号"]', 'i3t091@163.com')
    await iframe[1].type('[data-placeholder="密码"]', 'sas8723005')
    # 获取当前页面的元素总数
    login_len = await page.xpath('//*')
    print(len(login_len))
    print(type(len(login_len)))
    await iframe[1].click('[id="dologin"]')

    input()
    # 获取当前登录后的元素总数
    await asyncio.sleep(3)
    zk = await page.xpath('//*')
    print(len(zk))

    if len(login_len) == len(zk):
        print('出现滑块')
        # 进行滑块验证

        await iframe[1].click('[id="dologin"]')
    else:
        print('没有出现滑块')

    # 判断是否出现滑块
    # try:
    #     a = await iframe[1].waitForXPath('//span[@class="yidun_tips__text yidun-fallback__tip"]')
    #     print(a)
    #     a = await iframe[1].click('[class="yidun_tips__text yidun-fallback__tip"]')
    #     print(a)
    # except Exception:
    #     print('没有出现滑块验证')
    # else:
    #     print('出现滑块 进行滑块验证')
    #     # 再次点击登录
    #     await iframe[1].click('[id="dologin"]')
    # 滑块验证

    print('等待发布出现')
    await page.waitForXPath('//div[@id="root"]/div/div/div/div/div/ul/li/a[@class="menu-link"][@target="_self"]')
    # await asyncio.sleep(3)
    a = await page.click('#root > div > div > div > div > div > ul > li > a[target="_self"] ')
    # a = await page.click('#root > div > div > div > div > div > ul > li:nth-of-type(2) > a')
    print(a)
    print('进入发布页面')

    # 等待分类出现
    # await page.waitForXPath('')
    # 点击分类

    input()
    # await browser.close()


async def main():
    await run()


if __name__ == '__main__':
    asyncio.run(main())
